﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using WorkItemRSS.Properties;
using Rss;
using System.Data;

namespace WorkItemRSS
{
    /// <summary>
    /// 
    /// </summary>
    public class FeedHandler : IHttpHandler
    {
        #region IHttpHandler Members

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.
        /// </returns>
        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            //This will make sure the page output will be text/XML and not HTML
            context.Response.ContentType = "text/xml";
            context.Response.ContentEncoding = Encoding.UTF8;


            UserHostAddress = context.Request.UserHostAddress;

            if (string.IsNullOrEmpty(context.Request.QueryString["LogonUser"]))
                LogonUserIdentityName = context.Request.LogonUserIdentity.Name;
            else
                LogonUserIdentityName += "linkdotnet\\" + context.Request.QueryString["LogonUser"];

            SetAssignedTo(LogonUserIdentityName);
            if (!string.IsNullOrEmpty(AssignedTo))
            {
                RenderFeed(context);
            }
            else
            {
                NoFeeds(context);
            }
        }

        #endregion

        /// <summary>
        /// no new feeds found
        /// </summary>
        /// <param name="context">The context. object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        void NoFeeds(HttpContext context)
        {
            Feed oFeed = new Feed();
            context.Response.Write(oFeed.NoFeeds(ChannelName, tfsProjectName));
        }
        void SetAssignedTo(string LogonUserIdentityName)
        {
            Subscriber oSubscriber = new Subscriber();
            AssignedTo = oSubscriber.GetDisplayNameAndUpdateChart(LogonUserIdentityName);
        }
        /// <summary>
        /// Renders the feed.
        /// </summary>
        /// <param name="context">The context.</param>
        void RenderFeed(System.Web.HttpContext context)
        {
            try
            {
                WorkItemRSS.Feed oFeed = new WorkItemRSS.Feed();
                
                RssFeed rssFeed = null;
                oFeed.CreateRssFeed(out rssFeed, UserHostAddress, AssignedTo, WorkItemType, tfsProjectName, ChannelName);
                if (rssFeed != null)
                    rssFeed.Write(context.Response.OutputStream);
                else
                    NoFeeds(context);
            }
            catch
            {
                NoFeeds(context);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        string AssignedTo = string.Empty, UserHostAddress = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        public string ChannelName = string.Empty, WorkItemType = string.Empty, tfsProjectName = string.Empty, LogonUserIdentityName=string.Empty;
    }
}